$(function () {
    //获取url
    var url=localStorage.getItem("ajaxUrl");
    var pass=1;

    $.ajax({
        headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
        type: "POST",
        url: url + "/app/productinorder/getWareHouseForProductInRope",
        data: {},
        success: function (res) {
            pass = 1;
            loading_hide();
            if (res.success) {
                var data = res.result;
                var dataHtml = '<option value="">请选择入库仓库</option>';
                for (var i = 0; i < data.length; i++) {
                    dataHtml += '<option value="' + (data[i].warehouseName + ',' + data[i].warehouseCode) + '">' + data[i].warehouseName + '</option>';
                }
                $("div.div_piWh select.warehouse").html(dataHtml);
                var dataBm = res.department;
                var dataHtmlBm = '<option value="">请选择入库部门</option>';
                for (var i = 0; i < dataBm.length; i++) {
                    dataHtmlBm += '<option value="' + (dataBm[i].departmentName + ',' + dataBm[i].departmentCode) + '">' + dataBm[i].departmentName + '</option>';
                }
                $("div.div_piWh select.department").html(dataHtmlBm);
            } else {
                console.log(res);
                $alert(res.message);
            }
        },
        error: function (error) {
            pass = 1;
            loading_hide();
            console.log(error);
            $alert(error.responseText);
        }
    });

    // 选择 仓库
    $("div.div_piWh select.warehouse").on("change", function () {
        var warehouse = $("div.div_piWh select.warehouse").val();
        if (/电梯/.test(warehouse)) {
            $("div.div_piWh select.department").val("电梯绳合绳工作中心,F0203").trigger("change");
        } else if (/胶带/.test(warehouse)) {
            $("div.div_piWh select.department").val("胶带绳合绳工作中心,F0404").trigger("change");
        }
    });

    //点击 成品入库
    var type = "";
    $("li.productIn").on("click", function () {
        $(".mengceng").show();
        $("div.div_piWh").show();
        $("div.div_piWh p.p_tjh").hide();
        $("div.div_piWh select").val("").trigger("change");
        $("div.div_piWh input").val("");
        type = "productRope_one";
    });
    $("li.productPallet").on("click", function () {
        $(".mengceng").show();
        $("div.div_piWh").show();
        $("div.div_piWh p.p_tjh").show();
        $("div.div_piWh select").val("").trigger("change");
        $("div.div_piWh input").val("");
        type = "productPallet";
    });
    $("div.div_piWh .piWh_quxiao").on("click", function () {
        $(".mengceng").hide();
        $("div.div_piWh").hide();
        type = "";
    });
    $("div.div_piWh .piWh_queding").on("click", function () {
        var warehouse = $("div.div_piWh select.warehouse").val();
        var department = $("div.div_piWh select.department").val();
        var tjh = $("div.div_piWh input.tjh").val();
        if (!warehouse) {
            return $alert("请选择入库的仓库");
        } else if (!department) {
            return $alert("请选择入库的部门");
        } else if (type == "productPallet" && !tjh) {
            return $alert("请输入托架号");
        } else if (/电梯/.test(warehouse) && !/F0203/.test(department)) {
            return $alert("请检查部门");
        } else if (/胶带/.test(warehouse) && !/F0404/.test(department)) {
            return $alert("请检查部门");
        } else {
            localStorage.setItem("productRopeInWh", warehouse)
            localStorage.setItem("productRopeInDp", department)
            if (type == "productPallet") {
                localStorage.setItem("productRopeInTjh", tjh)
            }
            window.location.href = type + ".html";
        }
    });

});
